PROJECTENGRAM

STRINGS

Topic 2- Strings

Tags: #◇CCPROG2 #◇C Programming #◇Programming #◇Computer Science #◇Data Types #◇Data Structures #◇Structured Data #◇Structured Programming #◇Memory Management


◇Overview

In C, strings are null-terminated arrays of characters. There is no dedicated string type — strings are represented as char arrays ending with '\0'.

◇Key Concepts

A string is a char array terminated by '\0' String literals (e.g., "Hello") are stored in read-only memory The <string.h> library provides essential string functions scanf("%s", ...) stops at whitespace; fgets() reads full lines

◇Details / Theory

String Declaration and Initialization

『』
C
char str1[6] = "Hello";                // Size 6: 'H','e','l','l','o','\0'
char str2[] = "Hello";                  // Compiler infers size = 6
char str3[6] = {'H','e','l','l','o','\0'};  // Character-by-character
char *str4 = "Hello";                   // Pointer to string literal (read-only)

String Input/Output

『』
C
char name[50];

// scanf — stops at whitespace
scanf("%s", name);

// fgets — reads entire line (safer, includes newline)
fgets(name, sizeof(name), stdin);

// printf
printf("Name: %s\n", name);

// puts — prints string + newline
puts(name);

Common <string.h> Functions

FunctionPurposeExample
strlen(s)Length (excluding '\0')strlen("Hi") → 2
strcpy(dest, src)Copy src to deststrcpy(dest, "Hi")
strncpy(dest, src, n)Copy at most n charsSafer than strcpy
strcat(dest, src)Append src to deststrcat(buf, " World")
strncat(dest, src, n)Append at most n charsSafer than strcat
strcmp(a, b)Compare: 0 if equalstrcmp("ab", "ab") → 0
strncmp(a, b, n)Compare first n charsPartial comparison
strchr(s, c)Find first occurrence of cReturns pointer or NULL
strstr(s, sub)Find substringReturns pointer or NULL

String Comparison

『』
C
char a[] = "apple";
char b[] = "banana";

if (strcmp(a, b) < 0)
    printf("%s comes before %s\n", a, b);
// Cannot use == to compare strings; that compares addresses

String Manipulation Example

『』
C
#include <stdio.h>
#include <string.h>

int main() {
    char first[20] = "John";
    char last[20] = "Doe";
    char full[50];

    strcpy(full, first);   // full = "John"
    strcat(full, " ");     // full = "John "
    strcat(full, last);    // full = "John Doe"

    printf("Full name: %s (length: %lu)\n", full, strlen(full));
    return 0;
}

Character Functions (<ctype.h>)

FunctionDescription
isalpha(c)Is letter?
isdigit(c)Is digit?
isalnum(c)Is letter or digit?
toupper(c)Uppercase version
tolower(c)Lowercase version
isspace(c)Is whitespace?

◇Common Pitfalls

Using == to compare strings instead of strcmp(). Buffer overflow: writing more characters than array size allows. Forgetting the null terminator when building strings manually. scanf("%s", ...) is unsafe — no bounds checking. Prefer fgets(). Modifying a string literal via pointer (char *s = "abc"; s[0] = 'x'; → undefined behavior).

◇Summary

TL;DR — C strings are null-terminated char arrays. Use <string.h> functions (strlen, strcpy, strcmp, strcat) for manipulation. Always ensure buffers are large enough and terminated with '\0'. Never compare strings with ==.

Related: ◇What is an Array — ◇Declaration — ◇Accessing Elements — ◇Structs — ◇File Handling Source / Reference:

◇References & Navigation

CONFIRMATION // 理解度の確認

ACTIVE RECALL VERIFICATION

Select an answer to verify conceptual comprehension and reveal the technical rationale.

Q1

How are strings fundamentally represented in C?

Q2

What happens when you initialize a string as char str[] = "Hello";?

Q3

Which function from <string.h> is used to find the length of a string excluding the null terminator?

Q4

Why is using scanf("%s", name) generally considered unsafe or problematic?

Q5

Which function is recommended over scanf for reading full lines of string input safely?

Q6

What is the correct way to compare two strings in C?

Q7

If strcmp(a, b) returns a value of 0, what does it mean?

Q8

What is the primary difference between strcpy and strncpy?

Q9

Which header file contains functions like isalpha and isdigit for character manipulation?

Q10

What undefined behavior occurs if you attempt to modify a string literal like char *s = "abc"; s[0] = 'x';?